<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>目录读取</title>
    <script src='https://nginx-uat.crplingxi.com.cn/js/lib/jquery.min.js'></script>
    <script src='https://nginx-uat.crplingxi.com.cn/market/static/common/js/common.js'></script>
    <script src='https://nginx-uat.crplingxi.com.cn/market/static/common/js/util.js'></script>
</head>

<body>

    <!-- 使用 type="file" 和 webkitdirectory 实现选择目录 -->
    <input type="file" id="fileInput" webkitdirectory>

    <button class="save_button" onclick="load_files()">录入文件</button>

    <div id="output"></div>
    <script>
        const ajaxUrl = "https://nginx-uat.crplingxi.com.cn/";
        let projectData = {
            uids: '',
            project_name: '',
            project_address: '',
            company_name: '',
            project_price: '',
            project_time: '',
            project_text: '',
            project_detail: '',
            project_count: '',
            user_name: '',
            img_url: '',
            project_file: [],
            type_state: 0,
        };
        async function uploadShopDoc(file) {
            var len = file.name.split('.').length;
            const isLt2M = file.size / 1024 / 1024 < 10;
            if (!isLt2M) {
                console.error('上传文件大小不能超过 10MB!');
                return;
            } else {
                var fd = new FormData();
                fd.append('file', file);
                fd.append('businessType', 'projectText');
                fd.append('businessId', projectData.uids);
                $.ajax({
                    url: ajaxUrl + '/oss/filemag/upload',
                    type: 'POST',
                    data: fd,
                    dataType: 'JSON',
                    headers: {
                        "Authorization": "Bearer eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIxMDYyIiwidXNlckRhdGEiOm51bGwsInVzZXJObyI6bnVsbCwidW5pdERhdGEiOm51bGwsInNpdGVOYW1lIjpudWxsLCJzb3VyY2UiOm51bGwsIndvcmtObyI6bnVsbCwicG9zSWQiOm51bGwsInJvbGVQZXJtaXNzaW9uIjpudWxsLCJyb2xlSW5mbyI6bnVsbCwicm9sZURhdGEiOm51bGwsInVzZXJTdGF0ZSI6IjEiLCJ1bml0SWQiOm51bGwsInJvbGVHcm91cCI6IjI0ZWM0MzQwYjVhZTJmNzA2OWVhY2VmZTgzMzM0ZjdiMjBjYTYxODRkY2M3ZjY2ZWJkNTlmYTViNmUyYWVhMDEiLCJlbWFpbCI6Imd1b2RzZ2NjdGQuY29tIiwidW5pdE5hbWUiOm51bGwsImRhdGFJbmZvIjpudWxsLCJzaXRlRGF0YSI6bnVsbCwiZGVwdElkIjpudWxsLCJzaXRlSW5mbyI6bnVsbCwidXNlck5hbWUiOiLmtYvor5XnlKjmiLcxIiwidXNlcklkIjoiMTA2MiIsInNpdGVEb21haW4iOm51bGwsImRhdGFTaXRlIjpudWxsLCJwaG9uZSI6IjE2ODg4ODg4ODAxIiwibG9naW5BY3QiOm51bGwsInNpdGVJZCI6bnVsbCwidXNlclR5cGUiOm51bGwsImRhdGFQZXJtaXNzaW9uIjpudWxsLCJleHAiOjE3MTM1MjQ5MjF9.VDOfPyTeQ--95kEveP6weAfHqvMhA_DVFy1qiS-QNxR1iz0N-ItHMXMXD5qbVeERmng7aG-lKhZV75MbTsE-e-ZSpBHK7t3rbmJSeuMa3Ox1a8a65NAdESWYZvAWmdWVvLx-hSAaGt8KkKQfz5vHYV3IVlP25ep6rdpP5RZyTkk"
                    },
                    cache: false,
                    processData: false,
                    contentType: false,
                    success: function (result) {
                        if (typeof (result) == 'string') {
                            result = JSON.parse(result);
                        }
                        if (result.state == 0) {
                            console.error(result.msg);
                            return
                        }
                        var datamsg = {
                            //id:result.id,
                            name: file.name,
                            url: '/oss/filemag/getFileById?fileId=' + result.id
                        };
                        projectData.project_file.push(datamsg);
                        projectData.img_url = projectData.project_file;
                    }
                })
            }
        }

        // 提交
        function toSubmit(i) {
            projectData.type_state = i;
            projectData.project_count = 0;
            projectData.project_detail = projectData.project_name;
            var datamsg = {
                datamsg: JSON.stringify(projectData)
            };
            $.ajax({
                url: ajaxUrl + '/api/pbmag/dataManage/arithmeticSufferLib/control/addProjectText.do',
                type: 'POST',
                data: datamsg,
                headers: {
                    Authorization: "Bearer eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiIxMDYyIiwidXNlckRhdGEiOm51bGwsInVzZXJObyI6bnVsbCwidW5pdERhdGEiOm51bGwsInNpdGVOYW1lIjpudWxsLCJzb3VyY2UiOm51bGwsIndvcmtObyI6bnVsbCwicG9zSWQiOm51bGwsInJvbGVQZXJtaXNzaW9uIjpudWxsLCJyb2xlSW5mbyI6bnVsbCwicm9sZURhdGEiOm51bGwsInVzZXJTdGF0ZSI6IjEiLCJ1bml0SWQiOm51bGwsInJvbGVHcm91cCI6IjI0ZWM0MzQwYjVhZTJmNzA2OWVhY2VmZTgzMzM0ZjdiMjBjYTYxODRkY2M3ZjY2ZWJkNTlmYTViNmUyYWVhMDEiLCJlbWFpbCI6Imd1b2RzZ2NjdGQuY29tIiwidW5pdE5hbWUiOm51bGwsImRhdGFJbmZvIjpudWxsLCJzaXRlRGF0YSI6bnVsbCwiZGVwdElkIjpudWxsLCJzaXRlSW5mbyI6bnVsbCwidXNlck5hbWUiOiLmtYvor5XnlKjmiLcxIiwidXNlcklkIjoiMTA2MiIsInNpdGVEb21haW4iOm51bGwsImRhdGFTaXRlIjpudWxsLCJwaG9uZSI6IjE2ODg4ODg4ODAxIiwibG9naW5BY3QiOm51bGwsInNpdGVJZCI6bnVsbCwidXNlclR5cGUiOm51bGwsImRhdGFQZXJtaXNzaW9uIjpudWxsLCJleHAiOjE3MTM1MjQ5MjF9.VDOfPyTeQ--95kEveP6weAfHqvMhA_DVFy1qiS-QNxR1iz0N-ItHMXMXD5qbVeERmng7aG-lKhZV75MbTsE-e-ZSpBHK7t3rbmJSeuMa3Ox1a8a65NAdESWYZvAWmdWVvLx-hSAaGt8KkKQfz5vHYV3IVlP25ep6rdpP5RZyTkk"
                },
                success: function (res) {
                    if (res.success) {
                        console.log("提交成功！");
                    } else {
                        console.error(res.errMsg);
                    }
                }
            })
        }
        function delay(n) {
            return new Promise(function (resolve) {
                setTimeout(resolve, n * 1000);
            });
        }

        async function myAsyncFunction() {
            // Do what you want here
            console.log('Before the delay')

            await delay(5);

            console.log('After the delay')
            // Do what you want here too
        }
        function getSN() {
            var date = new Date()
            var s = date.getFullYear() +
                (date.getMonth() + 101 + '').substring(1) +
                (date.getDate() + 100 + '').substring(1) +
                (date.getHours() + 100 + '').substring(1) +
                (date.getMinutes() + 100 + '').substring(1) +
                (date.getSeconds() + 100 + '').substring(1) +
                (date.getMilliseconds() + 1000 + '').substring(1) +
                (Math.random() * 1000 + 1000).toFixed(0).substring(1)
            return s
        }
        function initData(fileName) {
            fileName = fileName.split('.')[0]
            projectData = {
                uids: '',
                project_name: fileName,
                project_address: '',
                company_name: '',
                project_price: '',
                project_time: '',
                project_text: '',
                project_detail: '',
                project_count: '',
                user_name: '',
                img_url: '',
                project_file: [],
                type_state: 0,
            }
            projectData.uids = getSN();
        }
        //因为载入文件是异步操作，函数必须采用asyn/await解决
        async function load_files() {
            var selectFiles = document.getElementById("fileInput").files;
            const output = document.querySelector('#output');
            files_content = [];

            for (var file of selectFiles) {
                /*                 const readFileAsync = file => new Promise(resolve => {
                                    const reader = new FileReader();
                                    reader.onload = evt => resolve(evt.target.result)
                                    reader.readAsText(file)
                
                                })
                                files_content.push(await readFileAsync(file)); */
                initData(file.name);
                await uploadShopDoc(file)
                await delay(2);
                toSubmit(2);
                await delay(2);
                const div = document.createElement('div');
                div.innerText = file.name;
                output.appendChild(div);
            }
            console.log(files_content);
        }
    </script>
</body>

</html>